package zouran.service.impl;

import org.springframework.stereotype.Service;
import zouran.entity.EarthquakeInfo;
import zouran.entity.IntensityLine;
import zouran.entity.SelectCondition;
import zouran.mapper.EarthquakeInfoMapper;
import zouran.mapper.IntensityLineMapper;
import zouran.service.EarthquakeInfoService;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author：zouran
 * Date：2023/3/31  20:57
 * Description:
 */
@Service
public class EarthquakeInfoServiceImpl implements EarthquakeInfoService {
    private final EarthquakeInfoMapper earthquakeInfoMapper;
    private final IntensityLineMapper intensityLineMapper;

    public EarthquakeInfoServiceImpl(EarthquakeInfoMapper earthquakeInfoMapper, IntensityLineMapper intensityLineMapper) {
        this.earthquakeInfoMapper = earthquakeInfoMapper;
        this.intensityLineMapper = intensityLineMapper;
    }

    @Override
    public EarthquakeInfo getEarthquakeInfoById(long id) {

        return earthquakeInfoMapper.getEarthquakeInfoById(id);
    }

    @Override
    public List<EarthquakeInfo> getAllEarthquakeInfo(Map<String, Object> parameter) {
        return earthquakeInfoMapper.getAllEarthquakeInfo(parameter);
    }

    @Override
    public List<EarthquakeInfo> getEarthquakeInfoByPosition(Double lon, Double lat) {
        return earthquakeInfoMapper.getEarthquakeInfoByPosition(lon, lat);
    }

    @Override
    public List<HashMap<String, Object>> getAllEarthquakeInfoPosition(Map<String, Object> parameter) {
        return earthquakeInfoMapper.getAllEarthquakePositionInfo(parameter);
    }

    @Override
    public List<EarthquakeInfo> getSeismicZonesEarthquakeInfo(Double east, Double south, Double west, Double north) {
        return earthquakeInfoMapper.getSeismicZonesEarthquakeInfo(east, south, west, north);
    }

    @Override
    public List<HashMap<String, Object>> getSeismicZonesEarthquakeInfoPosition(Double east, Double south, Double west, Double north) {
        return earthquakeInfoMapper.getSeismicZonesEarthquakePositionInfo(east, south, west, north);
    }

    @Override
    public List<EarthquakeInfo> getHistoryEarthquakeInfoList(String name) {
        return earthquakeInfoMapper.getHistoryEarthquakeInfoList(name);
    }

    @Override
    public Map<String, Object> getEarthquakeInfoByCondition(SelectCondition selectCondition) {
        Map<String, Object> map = new HashMap<>();
        map.put("s", selectCondition);
        Map<String, Object> objectMap = new HashMap<>();
        List<EarthquakeInfo> infoList = earthquakeInfoMapper.getAllEarthquakeInfo(map);
        objectMap.put("list", infoList);
        objectMap.put("count", infoList.size());
        objectMap.put("page", selectCondition.getPage() / selectCondition.getLimit() + 1);
        return objectMap;
    }

    public List<HashMap<String, Object>> getEarthquakePositionInfoByCondition(SelectCondition selectCondition) {
        Map<String, Object> map = new HashMap<>();
        map.put("s", selectCondition);
        return earthquakeInfoMapper.getAllEarthquakePositionInfo(map);
    }

    public boolean judgeEarthquakeInfoExist(String name, LocalDateTime time) {
        return earthquakeInfoMapper.judgeEarthquakeInfoExist(name, time) == 1;
    }

    @Override
    public boolean judgeEarthquakeInfoExistById(long id) {
        return earthquakeInfoMapper.judgeEarthquakeInfoExistById(id) == 1;
    }

    @Override
    public HashMap<String, Object> getEarthquakeIdRange() {
        return earthquakeInfoMapper.getEarthquakeIdRange();
    }

    @Override
    public List<Double> getEarthquakeInfoByyear(Integer year) {
        Integer nextyear = year+1;
        String year_start = year+"-01-01";
        String year_end = nextyear+"-01-01";
        System.out.println(year_start);
        System.out.println(year_end);
        return earthquakeInfoMapper.getEarthquakeStatisticByyear(year_start,year_end);

    }

    @Override
    public int insertEarthquakeInfo(EarthquakeInfo earthquakeInfo) {
        Double dividingLine = 105.0;
        if (earthquakeInfo.getLongitude() < dividingLine) {/*西部地区*/
            earthquakeInfo.getIntensity(5.253, 1.398, 4.164, 26, 0, 2.019, 1.398, 2.943, 8, 0);
        } else {/*东部地区*/
            earthquakeInfo.getIntensity(5.019, 1.446, 4.136, 24, 0, 2.240, 1.446, 3.070, 9, 0);
        }
        int result;
        result = earthquakeInfoMapper.insert(earthquakeInfo);
        for (IntensityLine intensityLine : earthquakeInfo.getIntensityLineList()) {
            intensityLine.setEarthquakeId(earthquakeInfo.getEarthquakeId());
            result = intensityLineMapper.insert(intensityLine);
        }
        return result;
    }
}
